$(document).ready(onLoginDocReady);

var selectedInputField;

function onLoginDocReady(){
	var doc = document;	
	handleLoginAndRegisterForm((doc.location.hash != "#register"));
}

function handleLoginAndRegisterForm(showLogin){
	if($('#loginForm').length > 0){	
		
		$('#registerForm input.tooltip_launch').prev().hide();
		$('#loginForm input.tooltip_launch').prev().hide();
		
		$('p.tooltip_content').hide();
		$('#registerForm input.tooltip_launch').bind("focus", onTooltipFocus);
		$('#loginForm input.tooltip_launch').bind("focus", onTooltipFocus);

		$('input.passw_text').show();
		$('input.passw_passw').hide();

		$('input.passw_text').focus(function() {
		    $(this).hide();
		    $(this).prev().show();
		    $(this).prev().focus();
		});
		$('input.passw_passw').blur(function() {
		    if($(this).val() == '') {
		        $(this).hide();
		        $(this).next().show();
		    }
		});
		
		$('#login input:not(input.passw_text)').autofill();		
		$('#register input:not(input.passw_text)').autofill();		
		
		$('#login a').bind('click', onLoginClicked);
		$('#register a:not(a.loginSucces)').bind('click', onRegisterClicked);
		
		$('#registerForm').hide();			
		$('#loginForm').hide();		
		if(showLogin){
			onLoginClicked();
		}else{
			onRegisterClicked();
		}
		
		// create an ajax form for login
		var options = {target:'#leftColumn', beforeSubmit:onSubmitLoginForm, success:onLoginLoaded};
		$('#loginForm').ajaxForm(options);
		
		// create an ajax form for login
		var options = {target:'#leftColumn', beforeSubmit:onSubmitRegisterForm, success:onRegisterLoaded};
		$('#registerForm').ajaxForm(options);
	}
	
}

function onSubmitLoginForm(formData, jqForm, options) { 
    var formElement = jqForm[0];
    return true; 
} 

function onLoginLoaded(){
	/*alert("onLogingLoaded");
	$('div.top_info').load("/top_info/");
	handleLoginAndRegisterForm(true);*/
	document.location.hash = "";
	document.location = "cart";
}
function onRegisterLoaded(){
	$('div.top_info').load("/top_info/");
	handleLoginAndRegisterForm(false);
	$('#registerForm a.loginSucces').bind('click', onLoginClicked);
}

function onSubmitRegisterForm(formData, jqForm, options) {
	var valid = true;
	var formElement = jqForm[0];
    var groupName = formElement[0];
    if(groupName.value == "Groupname" || groupName.value.length < 3){
    	$('#reg_groupname').addClass("error");
    	valid = false;
    }else{
    	$('#reg_groupname').removeClass("error");
    }

    var userName = formElement[1];
    if(userName.value == "Name" || userName.value.length < 3){
    	$('#reg_name').addClass("error");
    	valid = false;
    }else{
    	$('#reg_name').removeClass("error");
    }
    
    var email = formElement[2];
    if(email.value == "Email" || email.value.length < 3){
    	$('#reg_email').addClass("error");
    	valid = false;
    }else{
    	$('#email')[0].value = $('#reg_email')[0].value;
    	$('#email').css('color', '#333');    	
    	
    	$('#reg_email').removeClass("error");
    }
    
    var pass = formElement[3];
    if(pass.value.length < 4){
    	$('#reg_password').addClass("error");
    	$('#reg_password').next().addClass("error");
    	valid = false;
    }else{
    	$('#reg_password').removeClass("error");
    	$('#reg_password').next().removeClass("error");
    }

    var pass2 = formElement[5];
    if(pass2.value.length < 4){
    	$('#reg_password2').addClass("error");
    	$('#reg_password2').next().addClass("error");
    	valid = false;
    }else{
    	$('#reg_password2').removeClass("error");
    	$('#reg_password2').next().removeClass("error");
    }
    
    if(pass2.value != pass.value){
    	$('#reg_password').addClass("error");
    	$('#reg_password').next().addClass("error");
    	$('#reg_password2').addClass("error");
    	$('#reg_password2').next().addClass("error");
    	valid = false;
    }
    
	return valid;  
} 

function onLoginClicked(event){
	//event.preventDefault();
	$('#registerForm').slideUp(400);
	$('#loginForm').slideDown(400);

	if($('#email')[0].value == "Email"){
		$('#email').focus();		
	}else{
		$('#password').focus();		
	}
}
function onRegisterClicked(event){
	//event.preventDefault();
	$('#registerForm').slideDown(400);
	$('#loginForm').slideUp(400);
	
	$('#reg_groupname').focus();
}

function onTooltipFocus(e){
	selectedInputField = $(this);
	$('#registerForm input.tooltip_launch').prev().fadeOut(200);
	$('#loginForm input.tooltip_launch').prev().fadeOut(200);
	$('p.tooltip_content').fadeOut(200);
	setTimeout(onFadeOutPrevious, 201);
}

function onFadeOutPrevious(){
	var pTooltip = selectedInputField.next().next();
	var lLabel = selectedInputField.prev();
	
	lLabel.fadeIn(200);
	pTooltip.fadeIn(200);
}